<?php

namespace app\common\lib;

use think\facade\Db;

class Release
{
    public $web;

    public function run($param,$module)
    {
        if(!in_array($module,['article','project'])){
            return null;
        }
        $this->web = connect($param['webid']);
        if(empty($this->web)){
            return null;
        }

        switch ($this->web['cms_type']) {
            case 'destoon':
                $insertId = $this->destoon($param,$module);
            break;
            case 'eyoucms':
                $insertId = $this->eyoucms($param,$module);
            break;
            case 'dedecms':
                # code...
            break;
        }
        
        return $insertId;
    }

    private function destoon($param,$module)
    {
        switch ($module) {
            case 'article':
                $mod = Db::connect('dynamic')->name('module')->field('moduleid,module,name,moduledir')->find($this->web['cms_article']);
                $table = $mod['module'].'_'.$mod['moduleid'];
                $table_data = $mod['module'].'_data_'.$mod['moduleid'];
                $fields = Db::connect('dynamic')->name('fields')->field('name,default_value')->where('tb',$table)->select()->toArray();
                foreach($fields as $v){
                    $data[$v['name']] = $v['default_value'];
                }
                $data['catid'] = $param['catid'];
                $data['title'] = $param['title'];
                $data['introduce'] = $param['description'];
                $data['tag'] = $param['keywords'];
                $data['hits'] = mt_rand(300,500);
                $data['editor'] = $data['username'] = $param['username'];
                $data['addtime'] = $data['edittime'] = time();
                $data['ip'] = getClientIp();
                $data['status'] = 2;
                $data['subtitle'] = '';
                
                $data['dalei'] = get_typename($param['parent_typeid']);
                $data['xiaolei'] = get_typename($param['typeid']);
                $data['attr'] = $this->getTags($param);

                $pro_mod = Db::connect('dynamic')->name('module')->field('moduleid,module,name,moduledir')->find($this->web['cms_project']);
                $pro_table = $pro_mod['module'].'_'.$pro_mod['moduleid'];
                $project = Db::connect('dynamic')->name($pro_table)->field('itemid')->where('title',$param['project'])->find();
                if(!empty($project)){
                    $data['xmid'] = $project['itemid'];
                }

                $content = $param['content_view'];
                if(empty($content)){
                    $make = new \app\common\lib\MakeContent();
                    $make->dalei = $data['dalei'];
                    $make->xiaolei = $data['xiaolei'];
                    $make->xiangmu = $param['project'];
                    $make->parent_typeid = $param['parent_typeid'];
                    $make->typeid = $param['typeid'];
                    $content = $make->run($param['content']);
                }
                $content = $this->setContentPic($content);
                $data['thumb'] = $this->getThumb($content);

                $insertId = Db::connect('dynamic')->name($table)->insertGetId($data);
                if(empty($insertId)){
                    return null;
                }
                Db::connect('dynamic')->name($table)->where('itemid', $insertId)->update([
                    'linkurl' => $insertId.'.html'
                ]);
                $r = Db::connect('dynamic')->name($table_data)->replace()->insert([
                    'itemid' => $insertId,
                    'content' => $content
                ]);
                if($r){
                    http_request($this->web['url'].'/client_destoon.php?action=put_pic&itemid='.$insertId,[
                        'token:'.site_token(),
                    ]);
                }
                return $insertId;
            break;

            case 'project':
                
            break;
        }
    }

    private function eyoucms($param,$module){
        switch ($module) {
            case 'article':
                $mod = Db::connect('dynamic')->name('channeltype')->field('id,ntitle,table')->find($this->web['cms_article']);
                $table = $mod['table'].'_content';
                $fields = Db::connect('dynamic')->name('channelfield')->field('name,dfvalue')->where('channel_id',$mod['id'])->select()->toArray();
            break;

            case 'project':
                
            break;
        }
    }

    private function setContentPic($content){
        $domain = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
        $content = preg_replace('/(<img.+?src=")(.*?)/','$1'.$domain.'$2', $content);
        return $content;
    }

    private function getThumb($content){
        $pattern = "/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/";
        preg_match_all($pattern,$content,$match); 
        if(empty($match[1][0])){
            return '';
        }
        return $match[1][0];
    }

    private function getTags($param){
        //$fields = ['attr_ids','crowd_ids','amount_ids','region_ids','operate_ids'];
        $str = '';
        foreach($param as $k => $v){
            switch ($k) {
                case 'attr_ids':
                    $res = Db::name('data_tag_attr')->field('title')->where('id','in',$v)->select();
                    foreach($res as $kk => $vv){
                        $str .= $vv['title'].',';
                    }
                break;
                case 'crowd_ids':
                    $res = Db::name('data_tag_crowd')->field('title')->where('id','in',$v)->select();
                    foreach($res as $kk => $vv){
                        $str .= $vv['title'].',';
                    }
                break;
                case 'amount_ids':
                    $res = Db::name('data_tag_amount')->field('title')->where('id','in',$v)->select();
                    foreach($res as $kk => $vv){
                        $str .= $vv['title'].',';
                    }
                break;
                case 'region_ids':
                    $res = Db::name('data_tag_region')->field('title')->where('id','in',$v)->select();
                    foreach($res as $kk => $vv){
                        $str .= $vv['title'].',';
                    }
                break;
                case 'operate_ids':
                    $res = Db::name('data_tag_operate')->field('title')->where('id','in',$v)->select();
                    foreach($res as $kk => $vv){
                        $str .= $vv['title'].',';
                    }
                break;
            }
        }
        $str = rtrim($str, ',');
        return $str;
    }
    
}